//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ConstantSwaptionVolatility.h"
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/VolatilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
    CCalendar^ _Ccal;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantSwaptionVolatility = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        QuantLib::Volatility _volatility = (QuantLib::Volatility)ValueHelper::Convert (volatility);
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantSwaptionVolatility = new boost::shared_ptr<QuantLib::ConstantSwaptionVolatility> (new QuantLib::ConstantSwaptionVolatility ( _referenceDate,  _cal,  _bdc,  _volatility,  _dc ));
        SetVolatilityTermStructure (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
    CCalendar^ _Ccal;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantSwaptionVolatility = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        QuantLib::Volatility _volatility = (QuantLib::Volatility)ValueHelper::Convert (volatility);
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantSwaptionVolatility = new boost::shared_ptr<QuantLib::ConstantSwaptionVolatility> (new QuantLib::ConstantSwaptionVolatility ( _settlementDays,  _cal,  _bdc,  _volatility,  _dc ));
        SetVolatilityTermStructure (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
    CCalendar^ _Ccal;
    CQuote^ _Cvolatility;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantSwaptionVolatility = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantSwaptionVolatility = new boost::shared_ptr<QuantLib::ConstantSwaptionVolatility> (new QuantLib::ConstantSwaptionVolatility ( _referenceDate,  _cal,  _bdc,  _volatility,  _dc ));
        SetVolatilityTermStructure (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
    CCalendar^ _Ccal;
    CQuote^ _Cvolatility;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantSwaptionVolatility = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantSwaptionVolatility = new boost::shared_ptr<QuantLib::ConstantSwaptionVolatility> (new QuantLib::ConstantSwaptionVolatility ( _settlementDays,  _cal,  _bdc,  _volatility,  _dc ));
        SetVolatilityTermStructure (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (boost::shared_ptr<QuantLib::ConstantSwaptionVolatility>& childNative, Object^ owner) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
#ifdef HANDLE
	_phConstantSwaptionVolatility = NULL;
#endif
	_ppConstantSwaptionVolatility = &childNative;
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (QuantLib::ConstantSwaptionVolatility& childNative, Object^ owner) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
#ifdef HANDLE
	_phConstantSwaptionVolatility = NULL;
#endif
	_ppConstantSwaptionVolatility = new boost::shared_ptr<QuantLib::ConstantSwaptionVolatility> (&childNative);
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
    _ConstantSwaptionVolatilityOwner = owner;
    _VolatilityTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (CConstantSwaptionVolatility^ copy) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
#ifdef HANDLE
	_phConstantSwaptionVolatility = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstantSwaptionVolatility = new boost::shared_ptr<QuantLib::ConstantSwaptionVolatility> (copy->GetShared());
        _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (System::Type^ t) : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
#ifdef HANDLE
	_phConstantSwaptionVolatility = NULL;
#endif
	if (!t->IsSubclassOf(CConstantSwaptionVolatility::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (QuantLib::Handle<QuantLib::ConstantSwaptionVolatility>& childNative, Object^ owner)  : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
	_phConstantSwaptionVolatility = &childNative;
	_ppConstantSwaptionVolatility = &static_cast<boost::shared_ptr<QuantLib::ConstantSwaptionVolatility>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
    _ConstantSwaptionVolatilityOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (QuantLib::Handle<QuantLib::ConstantSwaptionVolatility> childNative)  : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
	_phConstantSwaptionVolatility = &childNative;
	_ppConstantSwaptionVolatility = &static_cast<boost::shared_ptr<QuantLib::ConstantSwaptionVolatility>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::CConstantSwaptionVolatility (QuantLib::ConstantSwaptionVolatility childNative)  : CVolatilityTermStructure(CConstantSwaptionVolatility::typeid)
{
#ifdef HANDLE
	_phConstantSwaptionVolatility = NULL;
#endif
	_ppConstantSwaptionVolatility = new boost::shared_ptr<QuantLib::ConstantSwaptionVolatility> (new QuantLib::ConstantSwaptionVolatility (childNative));
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppConstantSwaptionVolatility));
}
#endif

Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::~CConstantSwaptionVolatility ()
{
    if (_ppConstantSwaptionVolatility != NULL)
    {
	    delete _ppConstantSwaptionVolatility;
        _ppConstantSwaptionVolatility = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::!CConstantSwaptionVolatility ()
{
    if (_ppConstantSwaptionVolatility != NULL)
    {
	    delete _ppConstantSwaptionVolatility;
    }
}
QuantLib::ConstantSwaptionVolatility& Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::GetReference ()
{
    if (_ppConstantSwaptionVolatility == NULL) throw gcnew NativeNullException ();
	return **_ppConstantSwaptionVolatility;
}
boost::shared_ptr<QuantLib::ConstantSwaptionVolatility>& Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::GetShared ()
{
    if (_ppConstantSwaptionVolatility == NULL) throw gcnew NativeNullException ();
	return *_ppConstantSwaptionVolatility;
}
QuantLib::ConstantSwaptionVolatility* Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::GetPointer ()
{
    if (_ppConstantSwaptionVolatility == NULL) throw gcnew NativeNullException ();
	return &**_ppConstantSwaptionVolatility;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConstantSwaptionVolatility>& Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::GetHandle ()
{
	if (_phConstantSwaptionVolatility == NULL)
	{
		_phConstantSwaptionVolatility = new Handle<QuantLib::ConstantSwaptionVolatility> (*_ppConstantSwaptionVolatility);
	}
	return *_phConstantSwaptionVolatility;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::HasNative () 
{
	return (_ppConstantSwaptionVolatility != NULL);
}

DateTime Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppConstantSwaptionVolatility)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConstantSwaptionVolatility)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::MaxSwapTenor::get ()
{
    try
    {
    	QuantLib::Period& _rv = (QuantLib::Period&)(*_ppConstantSwaptionVolatility)->maxSwapTenor ( );   
        Cephei::QL::Times::CPeriod^ _nrv = gcnew Cephei::QL::Times::CPeriod (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConstantSwaptionVolatility)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Swaption::IConstantSwaptionVolatility^ Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility_Factory::Create (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return gcnew CConstantSwaptionVolatility ( referenceDate,  cal,  bdc,  volatility,  dc);
}
Cephei::QL::Termstructures::Volatility::Swaption::IConstantSwaptionVolatility^ Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return gcnew CConstantSwaptionVolatility ( settlementDays,  cal,  bdc,  volatility,  dc);
}
Cephei::QL::Termstructures::Volatility::Swaption::IConstantSwaptionVolatility^ Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility_Factory::Create (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return gcnew CConstantSwaptionVolatility ( referenceDate,  cal,  bdc,  volatility,  dc);
}
Cephei::QL::Termstructures::Volatility::Swaption::IConstantSwaptionVolatility^ Cephei::QL::Termstructures::Volatility::Swaption::CConstantSwaptionVolatility_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return gcnew CConstantSwaptionVolatility ( settlementDays,  cal,  bdc,  volatility,  dc);
}
